package com.kmxd.ams.infra.seal.setting;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import lombok.Data;
import lombok.experimental.Accessors;

/**
 * 数字设置工具
 *
 * @author zuolg
 */
@Data
@Accessors(chain = true)
public class ArchNumberUtil {

  /** 生成档号章 */
  public static BufferedImage creatArchSeal(NumberSetting setting, String num) {
    // 画布高度
    int imageHeight = setting.getHeight() + setting.getMarginY();
    // 画布宽度
    int imageWidth = setting.getFont().getSize() / 2 * num.length() + setting.getMarginX();

    BufferedImage image = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_RGB);
    Graphics2D g2d = image.createGraphics();
    // 默认白色背景
    g2d.fillRect(0, 0, imageWidth, imageHeight);
    // 设置背景透明
    image =
        g2d.getDeviceConfiguration()
            .createCompatibleImage(imageWidth, imageHeight, Transparency.TRANSLUCENT);
    g2d = image.createGraphics();
    // 设置线宽
    g2d.setStroke(new BasicStroke(setting.getLineWidth()));
    g2d.setColor(setting.getLineColor());

    // 抗锯齿（抗锯齿总开关）
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    // 消除文字锯齿，alpha差值（着色)
    g2d.setRenderingHint(
        RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
    // 文字抗锯齿
    g2d.setRenderingHint(
        RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_GASP);
    // 笔划规范化(画线用)
    g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);
    // 颜色规范化（渐变用）
    g2d.setRenderingHint(
        RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);
    // 字体小数开关
    g2d.setRenderingHint(
        RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
    // 插值算法，缩放旋转用
    g2d.setRenderingHint(
        RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
    // 呈现质量开关
    g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
    // 抖动形状
    g2d.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_ENABLE);

    // 3. 写入单元格内容
    g2d.setFont(setting.getFont());
    // 3. 写入单元格内容
    g2d.setColor(setting.getFontColor());

    // 写入内容
    g2d.drawString(num, setting.getMarginX() / 2, imageHeight / 2 + setting.getMarginX() / 2);
    g2d.drawImage(
        image.getScaledInstance(imageWidth, imageHeight, Image.SCALE_AREA_AVERAGING), 0, 0, null);
    g2d.dispose();
    return image;
  }

  public static void main(String[] args) throws IOException {
    String path = "D:\\tmp\\ams\\99.png";
    NumberSetting table =
        new NumberSetting()
            .setWidth(60)
            .setHeight(60)
            .setLineWidth(1)
            .setFontColor(Color.red)
            .setFont(new Font("宋体", Font.BOLD, 32));

    BufferedImage bufferedImage = ArchNumberUtil.creatArchSeal(table, "222");
    ImageIO.write(bufferedImage, "png", new File(path));
  }
}
